package com.leetcode.LC;

public class LC985 {
    public int[] sumEvenAfterQueries(int[] A, int[][] queries) {

        int[] ints = new int[A.length];
        int sum = 0;
        for (int i : A) {
            if (i % 2 == 0)
                sum += i;
        }

        for (int i = 0; i < queries.length; i++) {
            //三种情况
            /*
            原来为奇数，参数为奇数--》sum=sum+两个奇数和
            原来为奇数，参数为偶数--》sum不变
            原来为偶数，参数为偶数--》sum=sum+参数偶数
            原来为偶数，参数为奇数--》sum=sum-原来偶数
             */
            if (Math.abs(A[queries[i][1]]) % 2 == 1 && Math.abs(queries[i][0]) % 2 == 1) {

                sum = sum + A[queries[i][1]] + queries[i][0];
                A[queries[i][1]] += queries[i][0];
            } else if (Math.abs(A[queries[i][1]]) % 2 == 1 && Math.abs(queries[i][0]) % 2 == 0) {

                sum = sum;
                A[queries[i][1]] += queries[i][0];
            } else if (Math.abs(A[queries[i][1]]) % 2 == 0 && Math.abs(queries[i][0]) % 2 == 0) {

                sum = sum + queries[i][0];
                A[queries[i][1]] += queries[i][0];
            } else if (Math.abs(A[queries[i][1]]) % 2 == 0 && Math.abs(queries[i][0]) % 2 == 1) {

                sum = sum - A[queries[i][1]];
                A[queries[i][1]] += queries[i][0];
            }
            ints[i] = sum;
        }

        return ints;
    }

    public static void main(String[] args) {
        for (int i : new LC985().sumEvenAfterQueries(new int[]{1, 2, 3, 4}, new int[][]{{1, 0}, {-3, 1}, {-4, 0}, {2, 3}})) {
            System.out.print(i + ",");
        }
    }
}
